package com.cxb.demotest.mq.rocketmq.part_by_order;

import lombok.Data;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.ArrayList;
import java.util.List;

/**
 * 部分顺序消息生产者
 * @author Xb
 */
public class ProducerInOrder {
    public static void main(String[] args) throws Exception{
        DefaultMQProducer producer = new DefaultMQProducer("OrderProducer");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();
        //订单列表
        List<Order> orderList = buildOrders();
        for (int i = 0; i < orderList.size(); i++) {
            String body = orderList.get(i).toString();
            Message msg = new Message("PartOrder", null, "KEY" + i, body.getBytes());
            SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                    Integer id = (Integer) arg;
                    // 根据订单id选择发送queue
                    Integer index = id % mqs.size();
                    return mqs.get(index);
                }
            }, orderList.get(i).getId());
            System.out.println(String.format(
                    "SendResult status:%s, queueId:%d, body:%s",
                    sendResult.getSendStatus(),
                    sendResult.getMessageQueue().getQueueId(),
                    body));
        }
        producer.shutdown();

    }

    private static List<Order> buildOrders(){
        List<Order> orderList = new ArrayList<>();
        Order order = new Order();
        order.setId(1);
        order.setStatus("创建");
        orderList.add(order);

        order = new Order();
        order.setId(2);
        order.setStatus("创建");
        orderList.add(order);

        order = new Order();
        order.setId(1);
        order.setStatus("支付");
        orderList.add(order);

        order = new Order();
        order.setId(3);
        order.setStatus("创建");
        orderList.add(order);

        order = new Order();
        order.setId(1);
        order.setStatus("完成");
        orderList.add(order);

        order = new Order();
        order.setId(2);
        order.setStatus("支付");
        orderList.add(order);

        order = new Order();
        order.setId(3);
        order.setStatus("支付");
        orderList.add(order);

        order = new Order();
        order.setId(2);
        order.setStatus("完成");
        orderList.add(order);

        order = new Order();
        order.setId(3);
        order.setStatus("完成");
        orderList.add(order);

        return orderList;
    }

    /**
     * 订单的状态流转顺序为：创建》支付》完成
     */
    @Data
    protected static class Order{
        private int id;
        private String status;
    }

}
